// pages/Tabbar/Project/Project.js

//获取应用实例
const app = getApp()
import { queryProjectList } from '../../../api/project';
import { throttle } from '../../../utils/util';
import { Login } from '../../../store/user';

Page({
  stoptouchMove(){
    return false;
  },
  // 选择按钮事件
  myProjectStatus: function(e){
    let state = e.detail.state;
    console.log(state,'state')
    this.setData({
      state: state,
      noMore: false,
      page: 1,
    });
    this.requestData();
  },
  // 下拉刷新
  onRefresh(){
    this.setData({
      page : 1,
      noMore : false,
    })
     this.requestData();
  },
  // 跳转详情
  goDetail(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/Projects/ProjectDetail/ProjectDetail?projectId=${item.id}`,
    })
  },
  // 请求数据
  requestData(){
    let _this = this;
    queryProjectList(_this.data.state,_this.data.page)
    .then(res => {
      if(res.success){
        let list = _this.data.projectDatas;
        if(_this.data.page == 1){
          list = res.data
        }else{
          list.concat(res.data);
        }
        _this.setData({
          projectDatas: list,
          page: _this.data.page+1,
          triggered: false,
        })
        if((_this.data.page * 10) >= res.totalCount){
          _this.setData({
            noMore : true,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
   
  },
  // 加载更多
  loadMore:throttle(function(){
    this.requestData()
  },500),
  /**
   * 页面的初始数据
   */
  data: {
    widnowH:wx.getWindowInfo().windowHeight,
    status_Height:wx.getSystemInfoSync().statusBarHeight,
    triggered: false,
    isShowEmpty:false,
    projectDatas:[],
    token: wx.getStorageSync('token'),
    userRealnameStatus: wx.getStorageSync('realNameStatus'),
    page: 1,
    state: '',
    noMore: false,
    shareTitle: app.globalData.global.shareTitle,
    showPrivacyAuthor: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setStorageSync('toPath', '/pages/Tabbar/Project/Project')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    if(wx.getStorageSync('hasNewUser') == 'true'){
      await Login({noToast: false});
      wx.setStorageSync('hasNewUser', 'falses')
    }
    this.setData({
      token: wx.getStorageSync('token'),
      userRealnameStatus: wx.getStorageSync('realNameStatus'),
    })
    console.log(wx.getStorageSync('realNameStatus'),"wx.getStorageSync('realNameStatus')");
    if(wx.getStorageSync('realNameStatus') == '3'){
      this.requestData()
    }
    var _this = this;
    if (wx.onNeedPrivacyAuthorization){
      wx.onNeedPrivacyAuthorization((resolve) => {
        console.log('触发弹窗')
        // 触发弹窗
        _this.setData({
          showPrivacyAuthor: true,
        })
        _this.resolvePrivacyAuthorization = resolve
        console.log(resolve,resolve);
      })
    }  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Tabbar/Home/Home?scene='+userId,
    }
  },
  myChildEvent(e){
    console.log(e,'子组件触发')
    this.setData({
      token: wx.getStorageSync('token')
    })
  },
  // 获取项目列表数据
  getData(){
    
  },
  handleAgreePrivacyAuthorizationFun(){
    console.log('同意之后的回调');
  },
  disagreePrivacyAuthorization(){
    this.resolvePrivacyAuthorization({ event: 'disagree' });
  },
})